package com.sqoq.downloader3.ui

import androidx.core.view.isVisible
import com.bumptech.glide.Glide
import com.sqoq.downloader3.DownloadTool_ddg
import com.sqoq.downloader3.IntentUtilsl_ddg
import com.sqoq.downloader3.R
import com.sqoq.downloader3.base_ddg.BaseActivity_ddg
import com.sqoq.downloader3.bean_ddg.AnalysisVideoBean_ddg
import com.sqoq.downloader3.bean_ddg.VideoListBean_ddg
import com.sqoq.downloader3.databinding.ActivityDetaileDdgBinding
import com.sqoq.downloader3.getLanguageString_ddg
import com.sqoq.downloader3.lanuage_ddg.StringId_ddg
import com.sqoq.downloader3.videolist_ddg.VideoListActivity_ddg
import java.io.File

class DetaileActivity_ddg : BaseActivity_ddg<ActivityDetaileDdgBinding>() {

    lateinit var data: AnalysisVideoBean_ddg
    var filePath: VideoListBean_ddg? = null
    override val layout: Int
        get() = R.layout.activity_detaile_ddg

    override fun initView() {
        data = intent.getParcelableExtra("data")!!;
        Glide.with(baseContext).load(data.avatar_ss).into(binding.imgDdg)
        binding.ivBackDdg.setOnClickListener {
            finish()
        }
        binding.textDdg.setText(data.title_ss)
        binding.downloadDdg.setOnClickListener {
            startActivity(DownloadActivity_ddg::class.java)
        }
        binding.shareDdg.setOnClickListener {
            IntentUtilsl_ddg.shareVideo(baseContext, File(filePath?.filePath_ddg ?: ""), "")
        }
        filePath = DownloadTool_ddg.getDownLoadPath(data.url_ss!!)
        binding.btnDdg.text = getLanguageString_ddg(StringId_ddg.Download_ddg)
        filePath?.let {
            binding.shareDdg.isVisible = true
            binding.btnDdg.text = getLanguageString_ddg(StringId_ddg.Play_ddg)
        }
        binding.btnDdg.setOnClickListener {
            if (filePath != null) {
                VideoListActivity_ddg.start(baseContext, filePath!!)
                return@setOnClickListener
            }
            PermissionActivity_ddg.start(baseContext,1){
                binding.bottomProgress.isVisible = true
                binding.btnDdg.isVisible = false
                DownloadTool_ddg.download(this@DetaileActivity_ddg,
                    data.url_ss!!,
                    false,
                    onDownloadSuccess = {
                        filePath = it
                        binding.btnDdg.text = getLanguageString_ddg(StringId_ddg.Play_ddg)
                        binding.shareDdg.isVisible = true

                        binding.bottomProgress.isVisible = false
                        binding.btnDdg.isVisible = true
                    },
                    onProgress = {
                        binding.bottomProgress.progress = it
                    },
                    onDownloadFailed = {
                        binding.bottomProgress.isVisible = false
                        binding.btnDdg.isVisible = true
                    }, data.title_ss ?: ""
                )
            }

        }
    }
}